//导入题库的数据模型

const { T_exam_label } = require('../models/t_exam_label')

const { T_exam_questions_and_label } = require('../models/t_exam_questions_and_label')



//传输subid查找标签名
// 传输sub_id查找标签名（题库方面）
exports.getALLlabel = async (req, res) => {
    const { sub_id } = req.query;

    try {
        const T_label = await T_exam_label.findAll({
            attributes: ['label'],
            where: {
                sub_id: sub_id,
            }
        });

        if (T_label) {
            res.json({
                "code": 200,
                "msg": "查询成功",
                "data": T_label,
            });
        } else {
            res.json({
                "code": 404,
                "mag": "查询失败，该题库中的题目id不存在",
            });
        }
    } catch (error) {
        res.json({
            "code": 500,
            "msg": error.message
        });
    }
};

// 传输sub_id查找标签名（标签管理方面）
exports.getALLlabel1 = async (req, res) => {
    const { sub_id } = req.query;

    try {
        const T_label = await T_exam_label.findAll({
            where: {
                sub_id: sub_id,
            }
        });

        if (T_label) {
            res.json({
                "code": 200,
                "msg": "查询成功",
                "data": T_label,
            });
        } else {
            res.json({
                "code": 404,
                "mag": "查询失败，该题库中的题目id不存在",
            });
        }
    } catch (error) {
        res.json({
            "code": 500,
            "msg": error.message
        });
    }
};

//更新题库中的标签信息
exports.updatet_label = async (req, res) => {
    const id = req.query.id
    const label = req.query.label

    try {

        const [updateRows] = await T_exam_label.update(
            { label: label },
            {
                where: {
                    id: id,
                }
            },
        );

        res.json({
            "code": 1,
            "msg": "更新成功",
        });

    }
    catch (error) {
        res.json({
            "code": 500,
            "msg": error.message,
        })
    }
}

//插入标签
exports.createt_label = async (req, res) => {
    const body = req.body;
    try {
        const T_label = await T_exam_label.create({
            label: body.label,
            sub_id: body.sub_id,
        });

        res.json({
            "code": 1,
            "msg": '创建成功',
            "data": T_label,
        });
    } catch (error) {
        res.json({
            "code": 500,
            "msg": error.message,
        });
    }
};



//删除文章
exports.delete_labelById = async (req, res) => {
    const { id } = req.query;

    try {
        // 在删除标签之前查询是否有相关联的题目存在
        const relatedQuestions = await T_exam_questions_and_label.findOne({
            where: {
                label_id: id
            }
        });

        if (relatedQuestions) {
            res.json({
                'code': 400,
                'msg': '该标签已有题目存在，删除失败'
            });
        } else {
            const deletedRows = await T_exam_label.destroy({
                where: {
                    id: id
                }
            });

            if (deletedRows > 0) {
                res.json({
                    'code': 200,
                    'msg': '删除成功'
                });
            } else {
                res.json({
                    'code': 404,
                    'msg': '没有找到对应数据'
                });
            }
        }
    } catch (error) {
        console.error('删除数据失败:', error);
        res.json({
            'code': 500,
            'msg': '删除失败'
        });
    }
};
